package response

import (
	"cron_table_admin/global"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
)

type Response struct {
	Code int         `json:"code"`
	Data interface{} `json:"data"`
	Msg  string      `json:"msg"`
}

const (
	NOTLOGIN = 13
	EXPIRED  = 11
	ERROR    = 7
	SUCCESS  = 0
	WARNING  = 2
)

func Result(code int, data interface{}, msg string, c *gin.Context) {
	// 开始时间
	// 获取带 Context 的 logger
	log := global.LoggerWithContext(c)
	log.Info("requestResult", zap.Any("request path", c.Request.URL.Path), zap.Int("code", code), zap.String("msg", msg))
	c.JSON(http.StatusOK, Response{
		code,
		data,
		msg,
	})
}

func Ok(c *gin.Context) {
	Result(SUCCESS, map[string]interface{}{}, "success", c)
}

func OkWithMessage(message string, c *gin.Context) {
	Result(SUCCESS, map[string]interface{}{}, message, c)
}

func OkWithMessage2(message string, c *gin.Context) {
	Result(200, map[string]interface{}{}, message, c)
}

func OkWithData(data interface{}, c *gin.Context) {
	var resData string
	//ut,_:=json.Marshal(data)
	if env := global.VIPER_CONFIG.Get("system.is_decode"); env == true {
		//ut,_:=json.Marshal(data)
		//global.LOGGER.Info(string(ut))
		//resData = utils.MyDESEncrypt(ut)
		//params =  buf[0:n]
		Result(SUCCESS, resData, "success", c)
		//Result(SUCCESS, gin.H{"sign":resData}, "success", c)
	} else {
		//resData = string(ut)
		Result(SUCCESS, data, "success", c)
		//Result(SUCCESS, gin.H{"sign":resData}, "success", c)
	}
	//Result(SUCCESS, resData, "success", c)
}

func OkDetailed(data interface{}, message string, c *gin.Context) {
	Result(SUCCESS, data, message, c)
}

func Fail(c *gin.Context) {
	Result(ERROR, map[string]interface{}{}, "fail", c)
}

func FailWithMessage(message string, c *gin.Context) {
	Result(ERROR, map[string]interface{}{}, message, c)
}

func FailWithDetailed(code int, data interface{}, message string, c *gin.Context) {
	Result(code, data, message, c)
}
